package cn.iocoder.yudao.module.nmkj.service.companyintroduction;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.companyintroduction.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.companyintroduction.CompanyIntroductionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.companyintroduction.CompanyIntroductionMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMPANY_INTRODUCTION_NOT_EXISTS;

/**
 * 公司介绍 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CompanyIntroductionServiceImpl implements CompanyIntroductionService {

    @Resource
    private CompanyIntroductionMapper companyIntroductionMapper;

    @Override
    public Long createCompanyIntroduction(CompanyIntroductionSaveReqVO createReqVO) {
        resetEnabledStatusClose(createReqVO);
        // 插入
        CompanyIntroductionDO companyIntroduction = BeanUtils.toBean(createReqVO, CompanyIntroductionDO.class);
        companyIntroductionMapper.insert(companyIntroduction);
        // 返回
        return companyIntroduction.getId();
    }

    /**
     * 创建或修改功能，表中只允许只有一个开启状态的数据
     * 特别注意的是：调用需要判断当前插入或更新的值必须为开启状态
     */
    private void resetEnabledStatusClose(CompanyIntroductionSaveReqVO createReqVO) {
        if (createReqVO.getStatus() == null || createReqVO.getStatus() == 0) {
            return;
        }
        // 查询列表中有开启状态的一条数据即可，null 空则不进行设置
        CompanyIntroductionPageReqVO pageVO = new CompanyIntroductionPageReqVO();
        pageVO.setStatus(1);
        List<CompanyIntroductionDO> pageResult = companyIntroductionMapper.selectPage(pageVO).getList();
        // 判断是否有数据，则设置为关闭状态
        for (CompanyIntroductionDO companyIntroductionDO : pageResult) {
            companyIntroductionDO.setStatus(0); //设置状态
            companyIntroductionMapper.updateById(companyIntroductionDO); //更新数据库
        }
    }

    @Override
    public void updateCompanyIntroduction(CompanyIntroductionSaveReqVO updateReqVO) {
        resetEnabledStatusClose(updateReqVO);
        // 校验存在
        validateCompanyIntroductionExists(updateReqVO.getId());
        // 更新
        CompanyIntroductionDO updateObj = BeanUtils.toBean(updateReqVO, CompanyIntroductionDO.class);
        companyIntroductionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompanyIntroduction(Long id) {
        // 校验存在
        validateCompanyIntroductionExists(id);
        // 删除
        companyIntroductionMapper.deleteById(id);
    }

    private void validateCompanyIntroductionExists(Long id) {
        if (companyIntroductionMapper.selectById(id) == null) {
            throw exception(COMPANY_INTRODUCTION_NOT_EXISTS);
        }
    }

    @Override
    public CompanyIntroductionDO getCompanyIntroduction(Long id) {
        return companyIntroductionMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyIntroductionDO> getCompanyIntroductionPage(CompanyIntroductionPageReqVO pageReqVO) {
        return companyIntroductionMapper.selectPage(pageReqVO);
    }

    @Override
    public CompanyIntroductionDO getCompanyIntroductionSingle() {
        return companyIntroductionMapper.selectSingle();
    }

}